home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / The Timer and Time / DigitalClock / DigitalClock.cs next >
Encoding:
Text File  |  2001-01-15  |  1.6 KB  |  48 lines

  1. //-------------------------------------------
  2. // DigitalClock.cs ⌐ 2001 by Charles Petzold
  3. //-------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class DigitalClock: Form
  9. {
  10.      public static void Main()
  11.      {
  12.           Application.Run(new DigitalClock());
  13.      }
  14.      public DigitalClock()
  15.      {
  16.           Text = "Digital Clock";
  17.           BackColor = SystemColors.Window;
  18.           ForeColor = SystemColors.WindowText;
  19.           ResizeRedraw = true;
  20.           MinimumSize = SystemInformation.MinimumWindowSize + new Size(0, 1);
  21.  
  22.           Timer timer    = new Timer();
  23.           timer.Tick    += new EventHandler(TimerOnTick);
  24.           timer.Interval = 1000;
  25.           timer.Start();
  26.      }
  27.      private void TimerOnTick(object obj, EventArgs ea)
  28.      {
  29.           Invalidate();
  30.      }
  31.      protected override void OnPaint(PaintEventArgs pea)
  32.      {
  33.           Graphics grfx    = pea.Graphics;
  34.           string   strTime = DateTime.Now.ToString("T");
  35.           SizeF    sizef   = grfx.MeasureString(strTime, Font);
  36.           float    fScale  = Math.Min(ClientSize.Width  / sizef.Width,
  37.                                       ClientSize.Height / sizef.Height);
  38.           Font     font    = new Font(Font.FontFamily,
  39.                                       fScale * Font.SizeInPoints);
  40.  
  41.           sizef = grfx.MeasureString(strTime, font);
  42.  
  43.           grfx.DrawString(strTime, font, new SolidBrush(ForeColor), 
  44.                           (ClientSize.Width  - sizef.Width ) / 2, 
  45.                           (ClientSize.Height - sizef.Height) / 2);
  46.      }
  47. }
  48.